/* 
 * GWT-Ext Widget Library 
 * Copyright 2007 - 2008, GWT-Ext LLC., and individual contributors as indicated 
 * by the @authors tag. See the copyright.txt in the distribution for a 
 * full listing of individual contributors. 
 * 
 * This is free software; you can redistribute it and/or modify it 
 * under the terms of the GNU Lesser General Public License as 
 * published by the Free Software Foundation; either version 3 of 
 * the License, or (at your option) any later version. 
 * 
 * This software is distributed in the hope that it will be useful, 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 
 * Lesser General Public License for more details. 
 * 
 * You should have received a copy of the GNU Lesser General Public 
 * License along with this software; if not, write to the Free 
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org. 
 */

package edu.rpi.tw.pml.browsing.client;

import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.Record;
import com.gwtext.client.data.SimpleStore;
import com.gwtext.client.data.Store;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.Panel;
import com.gwtext.client.widgets.Toolbar;
import com.gwtext.client.widgets.ToolbarButton;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.grid.*;
import com.gwtext.client.widgets.layout.FitLayout;
import com.gwtext.client.widgets.layout.VerticalLayout;

import edu.rpi.tw.pml.browsing.client.util.JSONParser;

/**
 * A UI Widget that allows a user to select multiple values from a facet.
 */
public class FacetWidget {

	private final Panel panel = new Panel();
	private final FacetSearchServiceAsync proxy;
	private String facetName;
	private String[] facetValues;

	/*
	 * Default constructor.
	 */
	public FacetWidget() {
		this("Default");
	}

	/*
	 * Constructor with facet name.
	 * 
	 * @param title = facet name.
	 */
	public FacetWidget(String title) {
		facetName = title;

		panel.setBorder(false);
		panel.setLayout(new FitLayout()); // conflict w/ doLayout()

		proxy = GWT.create(FacetSearchService.class);
		proxy.getValuesByFacetName(facetName, new AsyncCallback<String>() {
			public void onFailure(Throwable caught) {
				System.out.println("Error while loading values for facet "
						+ facetName);
			}

			public void onSuccess(String result) {
				facetValues = JSONParser.string2FacetValueArray(result);
				buildGrid(facetValues);

			}
		});
	}

	private void buildGrid(String[] facetValues) {
		final CheckboxSelectionModel cbSelectionModel = new CheckboxSelectionModel();

		// String[] data = getFacetData();
		Store store = new SimpleStore("facet", facetValues);
		store.load();

		GridPanel grid = new GridPanel();
		grid.setStore(store);

		BaseColumnConfig[] columns = new BaseColumnConfig[] {
				new CheckboxColumnConfig(cbSelectionModel),
				new ColumnConfig("Select all?", "facet", 300, true, null,
						"column") };

		ColumnModel columnModel = new ColumnModel(columns);
		grid.setColumnModel(columnModel);

		grid.setFrame(true);
		grid.setStripeRows(true);
		grid.setAutoExpandColumn("column");

		grid.setSelectionModel(cbSelectionModel);
		grid.setWidth(385);
		grid.setHeight(300);
		grid.setFrame(true);
		grid.setTitle(facetName);
		grid.setCollapsible(true);
		grid.setIconCls("grid-icon");

		Toolbar bottomToolbar = new Toolbar();
		bottomToolbar.addFill();
		bottomToolbar.addButton(new ToolbarButton("Filter",
				new ButtonListenerAdapter() {
					public void onClick(Button button, EventObject e) {
						Record[] records = cbSelectionModel.getSelections();
						String[] msg = new String[records.length];
						String dlgmsg = "";
						for (int i = 0; i < records.length; i++) {
							Record record = records[i];
							msg[i] = record.getAsString("facet");
							dlgmsg += msg[i] + " and ";
						}
						MessageBox.alert("Records Selected: " + dlgmsg);
					}
				}));

		grid.setBottomToolbar(bottomToolbar);
		panel.add(grid);
		panel.doLayout();
	}

	public Panel getPanel() {
		return this.panel;
	}

	private String[] getFacetData() {
		return new String[] { "Jim Hendler", "Deborah McGuinness", "Li Ding",
				"Jie Bao", "Zhenning Shangguan", "Jiao Tao", "Rui Huang",
				"Jinguang Zheng", "Xixi Luo", "Jierui Xie", "Josh", "Gregory" };
	}
}
